package com.YOTO.httpstaticserver.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@Configuration
public class DatabaseInitializer {

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void initDatabase() {
        try {
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.setSeparator(";");
            populator.setContinueOnError(true);
            
            // 添加所有SQL脚本
            populator.addScript(new ClassPathResource("db/init.sql"));
            populator.addScript(new ClassPathResource("db/schema.sql"));
            populator.addScript(new ClassPathResource("db/data.sql"));
            
            populator.execute(dataSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 